﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Ecobee.Net.Demo
{
	public class CommandFactory : ICommandFactory
	{
		private readonly Dictionary<string, Type> _commands = new Dictionary<string, Type>();

		public Dictionary<string, Type> Commands
		{
			get
			{
				return _commands;
			}
		}

		public void RegisterCommand(Type command)
		{
			ICommand item = (ICommand)Activator.CreateInstance(command);
			this.Commands.Add(item.Key, command);
		}

		public ICommand CreateInstance(string key)
		{
			ICommand returnValue = null;

			var qry = from tbl in this.Commands
					  where tbl.Key.ToLower() == key.ToLower()
					  select tbl.Value;

			if (qry.Count() == 1)
			{
				returnValue = (ICommand)Activator.CreateInstance(qry.Single());
			}

			return returnValue;
		}
	}
}
